package L1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 一帮一
 */
public class _030 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N= Integer.valueOf(reader.readLine());
        List<String> girl = new ArrayList<>();
        List<String> boy = new ArrayList<>();
        List<String[]> list = new ArrayList<>();
        for (int i=0;i<N;i++){
            String[] s = reader.readLine().split(" ");
            list.add(s);
            if (s[0].equals("0")){
                girl.add(s[1]);
            }else{
                boy.add(s[1]);
            }
        }
        Collections.reverse(girl);
        Collections.reverse(boy);

        for (int i=0;i<N;i++){
            String[] s = list.get(i);
            if (s[0].equals("0")){
                if (boy.size()==0){
                    break;
                }
                System.out.println(s[1]+" "+boy.get(0));
                boy.remove(0);
                girl.remove(s[1]);
            }else{
                if (girl.size()==0)
                    break;
                System.out.println(s[1]+" "+girl.get(0));
                girl.remove(0);
                boy.remove(s[1]);
            }
        }

    }
}
